import Vue from "vue"
import Vuex from "vuex"
import test from "./test"

Vue.use(Vuex)
/**
 * 多组件公用数据时采用vuex，存储数据
 */
export default new Vuex.Store({
    //原始数据
    state: {
        count: 0,
        giftList: []
    },
    //提供修改数据的方法,不能在里面进行异步操作
    mutations: {
        addCount(state) {
            state.count++
        },
        setCount(state, newValue) {
            state.count = newValue
        },
        changeGiftList(state, newGiftList) {
            state.giftList = newGiftList
        }
    },
    //计算属性
    getters: {
        computeCount(state) {
            return "计算属性" + state.count
        }

    },
    //执行异步操作获取数据,修改数据的操作交给mutations中的方法
    actions: {
        loadGiftList($store) {
            let url = "http://localhost:9090/test/list"
            fetch(url).then(res => res.json()).then(res => {
                console.log(res.data)
                $store.commit("changeGiftList", res.data)
            })
        }
    },
    modules: {
        test
    }
})
